---
title: 'Read me for replication materials'
subtitle: 'Dür, Huber & Mateo _Compensating the Losers: The (Limited) Elite-Public Gap in Trade Politics_, *European Journal of Political Research*'
author: Andreas Dür, Robert A. Huber, and Gemma Mateo
output:
  rmdformats::downcute:
    use_bookdown: true
    self_contained: true
    thumbnails: false
    lightbox: true
    gallery: false
    highlight: monochrome
  editor_options:
  chunk_output_type: console
---

```{r globopt, include = F}
knitr::opts_chunk$set(warning = FALSE)
rm(list=ls())
```

This document guides through the replication for the following publication: Dür, Huber and Mateo 'Compensating the Losers: The (Limited) Elite-Public Gap in Trade Politics', forthcoming in *European Journal of Political Research*. Thank you for your interest in our manuscript and the underlying data.

# How to proceed

All scripts required to replicate the results in `R` can be run from the file `replication.R`. This file includes all analyses with the survey data necessary to replicate the paper. 

# Prepare Replication Data

After downloading our replication material from <https://doi.org/10.7910/DVN/VACXOD>, please open the file `EJPR_Replication_Materials.RProj`. You need to have installed `R` (version 4.5x) and `RStudio` for this. After opening the `.Rproj` file in `RStudio`, please open the file `replicaton.R` from the folder `rcode` within the project.

# Install/Load Packages

The following code installs all packages (if required). After that it loads the packages. The code then also uses the `here` package to set the working directory. Runtime of this code depends on the number of packages that you have already installed. However, even when you have no packages installed, it should only take a few minutes.

```{r pkgload, warning=FALSE, message=FALSE, results='hide'}
run_start_total <- Sys.time()
run_start <- Sys.time()

pkgs <- c("tidyverse",
          "labelled",
          "ordinal",
          "modelsummary",
          "ggstats",
          "here")

# Function to check if packages are installed
# If not: package will be installed from CRAN and then loaded
# If: Package will be loaed

install_load <- function(packages){
  
  for (p in packages) {
    cat("Check package: '", p, "'...\n", sep = "")
    flush.console()
    
    if (p %in% rownames(installed.packages())) {
      
      cat("Package: '", p, "' is already installed...\n\n", sep = "")
      flush.console()
      
      library(p, character.only=TRUE)
      
    } else {
      
      cat("Package: '", p, "' is NOT installed! Will install now...\n\n", sep = "")
      install.packages(p)
      library(p,character.only = TRUE)
      
    }
  }
  cat("\nAll packages installed!\n\n")
}

# Apply function to all required packages

install_load(pkgs)

# Set wd with here() package

here::i_am("EJPR_Replication_Materials.Rproj")
```


```{r}
run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```


# List of initial files

The files should cover the following:

```{r}
#List of files
list.files()
list.files(path = "./data/") #this data contains the processed data
list.files(path = "./rcode/") #This r-script contains the code for the analysis
```

# Analysis

This script `replication.R` provides analyses shown in the paper and the appendix. Its outputs are saved in the folders `figures` and `tables`. Due to data sensitivity, we cannot share some sensitive information on the legisators used in the analyses. The file `Analysis.html` provides a detailed log of the analysis.

```{r, include=T, warning=FALSE, message=FALSE, results='hide', fig.show='hide'}
run_start <- Sys.time()

source("rcode/replication.R", echo = T)

```

```{r}
run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# List of created files

The replication files should create the following:

```{r}
#List of files
list.files("./figures/")
list.files("./tables/")
```

# Session Info

This notebook was run using the following setup:

```{r}
pander::pander(sessionInfo())

run_stop_total <- Sys.time()
run_time_total <- (run_stop_total - run_start_total)
run_time_total
```
